/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.proteu.facesbeans.view.agenda;

import br.com.proteu.hibernate.beans.AgendaProteu;
import br.com.proteu.hibernate.util.FireBirdHelperProteu;
import br.com.proteu.util.RegexUtils;
import com.icesoft.faces.component.ext.HtmlCommandLink;
import java.io.Serializable;
import java.util.List;
import javax.faces.event.ActionEvent;

/**
 *
 * @author ricardo
 */
public class AgendaTelefonicaView implements Serializable{

    private AgendaProteu agendadoProteu;
    private List<AgendaProteu> listAgendaProteu;

    private String strPesquisaNome;

    private boolean readOnly = true;

    private String ageStNome;
    private String ageStCodigo;
    private String ageStCep;
    private String ageStEndereco;
    private String ageStEndNumero;
    private String ageStEndComplemento;
    private String ageStEndBairro;
    private String ageStEndCidade;
    private String ageStEndUF;
    private Integer ageInFoneDDD;
    private String ageStFone;
    private Integer ageInFone1DDD;
    private String ageStFone1;
    private Integer ageInFaxDDD;
    private String ageStFax;
    private Integer ageInCelDDD;
    private String ageStCel;
    private String ageStSite;
    private String ageStEmail;
    private String ageStContato;
    private String ageStFuncao;
    private String ageStObservacoes;
    

    public AgendaTelefonicaView(){
        init();
    }

    public void init(){
        grabListAgenda();
    }

    public boolean isReadOnly() {
        return readOnly;
    }

    public void setReadOnly(boolean readOnly) {
        this.readOnly = readOnly;
    }
    
    public String getStrPesquisaNome() {
        return strPesquisaNome;
    }

    public void setStrPesquisaNome(String strPesquisaNome) {
        this.strPesquisaNome = RegexUtils.spaceLessAndUpperCase(strPesquisaNome);
    }
    
    public Integer getAgeInCelDDD() {
        return ageInCelDDD;
    }

    public void setAgeInCelDDD(Integer ageInCelDDD) {
        this.ageInCelDDD = ageInCelDDD;
    }

    public Integer getAgeInFaxDDD() {
        return ageInFaxDDD;
    }

    public void setAgeInFaxDDD(Integer ageInFaxDDD) {
        this.ageInFaxDDD = ageInFaxDDD;
    }

    public Integer getAgeInFone1DDD() {
        return ageInFone1DDD;
    }

    public void setAgeInFone1DDD(Integer ageInFone1DDD) {
        this.ageInFone1DDD = ageInFone1DDD;
    }

    public Integer getAgeInFoneDDD() {
        return ageInFoneDDD;
    }

    public void setAgeInFoneDDD(Integer ageInFoneDDD) {
        this.ageInFoneDDD = ageInFoneDDD;
    }

    public String getAgeStCel() {
        return ageStCel;
    }

    public void setAgeStCel(String ageStCel) {
        this.ageStCel = ageStCel;
    }

    public String getAgeStCep() {
        return ageStCep;
    }

    public void setAgeStCep(String ageStCep) {
        this.ageStCep = ageStCep;
    }

    public String getAgeStCodigo() {
        return ageStCodigo;
    }

    public void setAgeStCodigo(String ageStCodigo) {
        this.ageStCodigo = ageStCodigo;
    }

    public String getAgeStContato() {
        return ageStContato;
    }

    public void setAgeStContato(String ageStContato) {
        this.ageStContato = RegexUtils.spaceLessAndUpperCase(ageStContato);
    }

    public String getAgeStEmail() {
        return ageStEmail;
    }

    public void setAgeStEmail(String ageStEmail) {
        this.ageStEmail = RegexUtils.spaceLessAndUpperCase(ageStEmail);
    }

    public String getAgeStEndBairro() {
        return ageStEndBairro;
    }

    public void setAgeStEndBairro(String ageStEndBairro) {
        this.ageStEndBairro = RegexUtils.spaceLessAndUpperCase(ageStEndBairro);
    }

    public String getAgeStEndCidade() {
        return ageStEndCidade;
    }

    public void setAgeStEndCidade(String ageStEndCidade) {
        this.ageStEndCidade = RegexUtils.spaceLessAndUpperCase(ageStEndCidade);
    }

    public String getAgeStEndComplemento() {
        return ageStEndComplemento;
    }

    public void setAgeStEndComplemento(String ageStEndComplemento) {
        this.ageStEndComplemento = RegexUtils.spaceLessAndUpperCase(ageStEndComplemento);
    }

    public String getAgeStEndNumero() {
        return ageStEndNumero;
    }

    public void setAgeStEndNumero(String ageStEndNumero) {
        this.ageStEndNumero = ageStEndNumero;
    }

    public String getAgeStEndUF() {
        return ageStEndUF;
    }

    public void setAgeStEndUF(String ageStEndUF) {
        this.ageStEndUF = ageStEndUF;
    }

    public String getAgeStEndereco() {
        return ageStEndereco;
    }

    public void setAgeStEndereco(String ageStEndereco) {
        this.ageStEndereco = RegexUtils.spaceLessAndUpperCase(ageStEndereco);
    }

    public String getAgeStFax() {
        return ageStFax;
    }

    public void setAgeStFax(String ageStFax) {
        this.ageStFax = ageStFax;
    }

    public String getAgeStFone() {
        return ageStFone;
    }

    public void setAgeStFone(String ageStFone) {
        this.ageStFone = ageStFone;
    }

    public String getAgeStFone1() {
        return ageStFone1;
    }

    public void setAgeStFone1(String ageStFone1) {
        this.ageStFone1 = ageStFone1;
    }

    public String getAgeStFuncao() {
        return ageStFuncao;
    }

    public void setAgeStFuncao(String ageStFuncao) {
        this.ageStFuncao = RegexUtils.spaceLessAndUpperCase(ageStFuncao);
    }

    public String getAgeStObservacoes() {
        return ageStObservacoes;
    }

    public void setAgeStObservacoes(String ageStObservacoes) {
        this.ageStObservacoes = RegexUtils.spaceLessAndUpperCase(ageStObservacoes);
    }

    public String getAgeStSite() {
        return ageStSite;
    }

    public void setAgeStSite(String ageStSite) {
        this.ageStSite = RegexUtils.spaceLessAndUpperCase(ageStSite);
    }

    public AgendaProteu getAgendadoProteu() {
        return agendadoProteu;
    }

    public void setAgendadoProteu(AgendaProteu agendadoProteu) {
        this.agendadoProteu = agendadoProteu;
    }

    public String getAgeStNome() {
        return ageStNome;
    }

    public void setAgeStNome(String ageStNome) {
        this.ageStNome = RegexUtils.spaceLessAndUpperCase(ageStNome);
    }

    public void setListAgendaProteu(List<AgendaProteu> listAgendaProteu) {
        this.listAgendaProteu = listAgendaProteu;
    }

    public List<AgendaProteu> getListAgendaProteu() {
        return listAgendaProteu;
    }

    

    private void everything(){
        if(agendadoProteu != null){
            ageStNome = agendadoProteu.getAgeStNome();
            ageStCodigo = agendadoProteu.getAgeStCodigo();
            ageStCep = agendadoProteu.getAgeStCep();
            ageStEndereco = agendadoProteu.getAgeStEndereco();
            ageStEndNumero = agendadoProteu.getAgeStEndNumero();
            ageStEndComplemento = agendadoProteu.getAgeStEndComplemento();
            ageStEndBairro = agendadoProteu.getAgeStEndBairro();
            ageStEndCidade = agendadoProteu.getAgeStEndCidade();
            ageStEndUF = agendadoProteu.getAgeStEndUF();
            ageInFoneDDD = agendadoProteu.getAgeInFoneDDD();
            ageStFone = agendadoProteu.getAgeStFone();
            ageInFone1DDD = agendadoProteu.getAgeInFone1DDD();
            ageStFone1 = agendadoProteu.getAgeStFone1();
            ageInFaxDDD = agendadoProteu.getAgeInFaxDDD();
            ageStFax = agendadoProteu.getAgeStFax();
            ageInCelDDD = agendadoProteu.getAgeInCelDDD();
            ageStCel = agendadoProteu.getAgeStCel();
            ageStSite = agendadoProteu.getAgeStSite();
            ageStEmail = agendadoProteu.getAgeStEmail();
            ageStContato = agendadoProteu.getAgeStContato();
            ageStFuncao = agendadoProteu.getAgeStFuncao();
            ageStObservacoes = agendadoProteu.getAgeStObservacoes();
        }
    }
    private void everythingToNull(){
            ageStNome = null;
            ageStCodigo = null;
            ageStCep = null;
            ageStEndereco = null;
            ageStEndNumero = null;
            ageStEndComplemento = null;
            ageStEndBairro = null;
            ageStEndCidade = null;
            ageStEndUF = null;
            ageInFoneDDD = null;
            ageStFone = null;
            ageInFone1DDD = null;
            ageStFone1 = null;
            ageInFaxDDD = null;
            ageStFax = null;
            ageInCelDDD = null;
            ageStCel = null;
            ageStSite = null;
            ageStEmail = null;
            ageStContato = null;
            ageStFuncao = null;
            ageStObservacoes = null;
    }

    private AgendaProteu buildAgendado(AgendaProteu a){

       a.setAgeInCelDDD(ageInCelDDD);
       a.setAgeInFaxDDD(ageInFaxDDD);
       a.setAgeInFone1DDD(ageInFone1DDD);
       a.setAgeInFoneDDD(ageInFoneDDD);
       a.setAgeStCel(ageStCel);
       a.setAgeStCep(ageStCep);
//       a.setAgeStCodigo(ageStCodigo);
       a.setAgeStContato(ageStContato);
       a.setAgeStEmail(ageStEmail);
       a.setAgeStEndBairro(ageStEndBairro);
       a.setAgeStEndCidade(ageStEndCidade);
       a.setAgeStEndComplemento(ageStEndComplemento);
       a.setAgeStEndNumero(ageStEndNumero);
       a.setAgeStEndUF(ageStEndUF);
       a.setAgeStEndereco(ageStEndereco);
       a.setAgeStFax(ageStFax);
       a.setAgeStFone(ageStFone);
       a.setAgeStFone1(ageStFone1);
       a.setAgeStFuncao(ageStFuncao);
       a.setAgeStNome(ageStNome);
       a.setAgeStObservacoes(ageStObservacoes);
       a.setAgeStSite(ageStSite);
       return a;
       }

    public List grabListAgenda(){
        this.listAgendaProteu = FireBirdHelperProteu.getListObject(AgendaProteu.class);
        return listAgendaProteu;
    }

    public void grabAgendado(ActionEvent event){
        if(event != null){
            HtmlCommandLink commandLink = (HtmlCommandLink) event.getComponent();
            String ageStNome = commandLink.getLang();
            if(ageStNome != null){
                for(AgendaProteu a : listAgendaProteu){
                    if(a.getAgeStNome().equalsIgnoreCase(ageStNome)){
                        this.agendadoProteu = a;
                        this.everything();
                        this.listAgendaProteu.clear();
                        break;
                    }
                }
            }
        }
    }
    

    public List grabListAgendadoByNome(){
        listAgendaProteu.clear();
        this.listAgendaProteu = FireBirdHelperProteu.getListAgendaProteuByNome(ageStNome);
        return listAgendaProteu;
    }

    public void incluirAgendado(){
        everythingToNull();
        readOnly = false;
    }

    public void alterarAgendado(){
        readOnly = false;
    }

    public void salvarAgendado(){
        if(readOnly == false){
            if(this.agendadoProteu == null){
                this.agendadoProteu = new AgendaProteu();
            }
            this.buildAgendado(agendadoProteu);

            if(this.agendadoProteu.getAgeStCodigo() != null){
                FireBirdHelperProteu.updateObject(this.agendadoProteu);
            }else{
                this.agendadoProteu.setAgeStCodigo(FireBirdHelperProteu.getAgendaNextValue());
                if(this.agendadoProteu.getAgeStCodigo() != null){
                    FireBirdHelperProteu.saveObject(this.agendadoProteu);
                }
                
            }
        }
        readOnly = true;
    }

    public void cancelarAgendamento(){
        readOnly = true;
    }
    
}
